package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 会员统计服务提供者
 * @author zhengjd
 * @date 2020/4/12 10:00
 */
@Transactional
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    MemberDao memberDao;

    @Autowired
    SetmealDao setmealDao;

    @Autowired
    OrderDao orderDao;


    /**
     * 根据月份获取每月会员数量
     * @param dateList
     * @return
     */
    @Override
    public List<Integer> getMemberCountByMonth(ArrayList<String> dateList) {
        if (dateList != null && dateList.size()>0){
            List<Integer> memberCount = new ArrayList<>();
            for (String month : dateList) {
                month = month + "-31";
                int count = memberDao.getMemberCountByMonth(month);
                memberCount.add(count);
            }
            return memberCount;
        }
        return null;
    }

    /**
     * 获取所有套餐和套餐数量
     * @return
     */
    @Override
    public Map getSetmealReport() {
        List<Setmeal> setmeals = setmealDao.getSetmeal();
        if (setmeals!=null && setmeals.size() > 0){
            //套餐名集合
            List<String> setmealNames = new ArrayList<>();
            //套餐预约数量集合
            List<Map> setmealCount = new ArrayList<>();
            for (Setmeal setmeal : setmeals) {
                setmealNames.add(setmeal.getName());
                Map<String, Object> countMap = new HashMap<>();
                //获取套餐的预约数量
                int count = orderDao.getSetmealCountBySetmealId(setmeal.getId());
                countMap.put("value",count);
                countMap.put("name",setmeal.getName());
                setmealCount.add(countMap);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("setmealNames",setmealNames);
            map.put("setmealCount",setmealCount);
            return map;
        }
        return null;
    }

    /**
     * 运营数据统计
     * @return
     */
    @Override
    public Map getBusinessReportData() throws Exception {

        //获取当前日期
        String reportDate = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本周日的日期
        String thisWeekSunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());

        //获取本月的第一天
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //获取本月的最后一天
        String lastDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());

        //新增会员数
        long todayNewMember = memberDao.findMemberCountByDate(reportDate);
        //总会员数
        long totalMember =  memberDao.findTotalMember();
        //本周新增会员数
        long thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        long thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);
        //今日预约数
        long todayOrderNumber = orderDao.findTodayOrderNumber(reportDate);
        //今日到诊数
        long todayVisitsNumber = orderDao.findTodayVisitsNumber(reportDate);
        //本周预约数
        Map<String, String> weekMap = new HashMap<>();
        weekMap.put("begin",thisWeekMonday);
        weekMap.put("ending",thisWeekSunday);
        long thisWeekOrderNumber = orderDao.findOrderCountBetweenDate(weekMap);
        //本周到诊数
        long thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(weekMap);
        //本月预约数
        Map<String,String> monthMap = new HashMap<>();
        monthMap.put("begin",firstDay4ThisMonth);
        monthMap.put("ending",lastDay4ThisMonth);
        long thisMonthOrderNumber = orderDao.findOrderCountBetweenDate(monthMap);
        //获取本月到诊数
        long thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(monthMap);
        //获取前4个热门套餐
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        Map<String, Object> map = new HashMap<>();
        map.put("reportDate",reportDate);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }


}
